<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProfileForm
 *
 * @author Sorasak Siangsanan
 */
class ProfileForm extends CFormModel {

    public $username;
    public $password;
    public $repassword;
    public $firstname;
    public $lastname;
    public $height;
    public $weight;
    public $gender;
    public $birthday;
    public $time_of_birthday;
    public $mobilephone;
    public $profile_image;
    public $isChangPassword;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            // username and password are required
            array('firstname, lastname, gender, birthday, mobilephone', 'required'),
            // compare password and repeat password
            array('repassword', 'compare', 'compareAttribute' => 'password', 'on' => 'changePassword'),
            // numeric with decimal only
            array('height', 'match', 'pattern' => '/^[0-9]{1,12}(\.[0-9]{0,4})?$/'),
            array('weight', 'match', 'pattern' => '/^[0-9]{1,12}(\.[0-9]{0,4})?$/'),
            // numeric only
            array('mobilephone', 'numerical', 'integerOnly' => true),
            // check types
            array('profile_image', 'file', 'allowEmpty' => true, 'types' => 'jpg, gif, png, jpeg'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'username' => Yii::t('register', 'email'),
            'password' => Yii::t('register', 'password'),
            'repassword' => Yii::t('register', 'repassword'),
            'firstname' => Yii::t('register', 'firstname'),
            'lastname' => Yii::t('register', 'lastname'),
            'height' => Yii::t('register', 'height'),
            'weight' => Yii::t('register', 'weight'),
            'gender' => Yii::t('register', 'gender'),
            'birthday' => Yii::t('register', 'birthday'),
            'time_of_birthday' => Yii::t('register', 'time_of_birthday'),
            'mobilephone' => Yii::t('register', 'mobilephone'),
            'profile_image' => Yii::t('register', 'profile_image'),
        );
    }

    public function profile($customer) {

        if ($this->isChangPassword) {
            $customer->password = $this->password;
        }

        $customer->Name = $this->firstname;
        $customer->Surname = $this->lastname;
        $customer->height = $this->height;
        $customer->weight = $this->weight;
        $customer->sex = $this->gender;
        $customer->birthdate = ConvertFormatUtil::BEStringToADString($this->birthday, false);
        if (isset($this->time_of_birthday)) {
            $customer->birthdate .= ' ' . $this->time_of_birthday;
        } else {
            $customer->birthdate .= ' 00:00:00';
        }
        $customer->telephone = $this->mobilephone;

        $uploadedFile = CUploadedFile::getInstance($this, 'profile_image');
        if (isset($uploadedFile)) {
            @unlink(Yii::app()->basePath . '/../' . $customer->picture);
//            $filename = substr($uploadedFile->name, 0, strrpos($uploadedFile->name, '.'));
            $filename = ImageUtil::PREFIX_PROFILE;
            $filepath = 'userimages/customer/' . date('Y-m-d_his') . $filename . '.' . $uploadedFile->extensionName;
            $customer->picture = $filepath;
        }

        $customer->update_date = date('Y-m-d H:i:s');
        if ($customer->update()) {
            if (!empty($uploadedFile)) {  // check if uploaded file is set or not
                $filename = substr($uploadedFile->name, 0, strrpos($uploadedFile->name, '.'));
                $uploadedFile->saveAs($filepath);
            }
            return true;
        }
        return false;
    }

}
